import { message, Modal } from "ant-design-vue";
import dayjs from "dayjs";

/**
 * 开启Modal
 * @param {*} title
 * @param {*} content
 * @param {*} onOk
 * @param {*} onCancel
 */
export const openModal = (obj = {}, onOk = false, onCancel = false, modalType = "warning") => {
	Modal[modalType]({
		title: () => obj.title || "温馨提示",
		content: () => obj.content,
		...obj,
		okText: () => "确定",
		cancelText: () => "取消",
		onOk() {
			if (onOk) {
				onOk();
			}
		},
		onCancel() {
			if (onCancel) {
				onCancel();
			}
		},
	});
};

/**
 * 开启Message
 * @param {*} content
 * @param {*} type
 * @param {*} callback
 * @param {*} duration
 */
export const openMessage = (
	content = "",
	type = "info",
	callback = false,
	duration = 3,
	key = ""
) => {
	switch (type) {
		case "success":
			message.success({
				content,
				duration,
				key,
				class: "l-coustom-message-success",
				onClose: () => {
					if (callback) {
						callback();
					}
				},
			});
			break;
		case "error":
			message.error({
				content,
				duration,
				key,
				onClose: () => {
					if (callback) {
						callback();
					}
				},
			});
			break;
		case "loading":
			message.loading({
				content,
				duration,
				key,
				onClose: () => {
					if (callback) {
						callback();
					}
				},
			});
			break;
		default:
			message.info({
				content,
				duration,
				key,
				onClose: () => {
					if (callback) {
						callback();
					}
				},
			});
			break;
	}
};

/** 
	*获取文件
	* @param {String} url  assets 后文件路径
*/
export const getAssetsFile = (url) => {
	return new URL(`/src/assets/${url}`, import.meta.url).href
}


/**
 * 生成一个符合UUID版本4标准的随机唯一标识符。
 *
 * UUID（通用唯一标识符）是一种128位的数字，通常用16进制表示，
 * 格式为8-4-4-4-12的32个字符，本函数生成的UUID中包含了版本4所需的变体和版本号。
 *
 * @returns {String} 返回一个格式化的UUID字符串，如："xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"，
 *                  其中x表示任何十六进制数字，y表示8、9、a或b中的一个，以符合版本4的规范。
 */
export function generateUUID() {
	return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
		var r = (Math.random() * 16) | 0,
			v = c === "x" ? r : (r & 0x3) | 0x8;
		return v.toString(16);
	});
}


/**
 * 节流函数
 */
export const throttle = (fn, delay = 500) => {
	let timer = null;
	return function () {
		if (timer) {
			return;
		}
		timer = setTimeout(() => {
			fn.apply(this, arguments);
			timer = null;
		}, delay);
	};
};
/**
 * @description 获取合并单元格rowSpan集合
 * @param {Array} dataScroce 数据源
 * @param {String} filed 需要合并的字段
 * @returns {Array} 该字段下单元格rowSpan集合
 */
export const getRowspan = (dataScroce, filed) => {
	let spanArr = [];
	let position = 0;
	dataScroce.forEach((item, index) => {
		if (index === 0) {
			spanArr.push(1);
			position = 0;
		} else {
			//需要合并的地方判断
			if (dataScroce[index][filed] === dataScroce[index - 1][filed]) {
				spanArr[position] += 1;
				spanArr.push(0);
			} else {
				spanArr.push(1);
				position = index;
			}
		}
	});
	return spanArr;
};